package com.torlesse.authdemo.controller;

import com.torlesse.authdemo.common.annotation.RoleAuth;
import com.torlesse.authdemo.common.resp.Result;
import com.torlesse.authdemo.pojo.User;
import com.torlesse.authdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/02/19/9:59
 * @Description:用户管理控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RoleAuth(value = "ROLE_ADMIN")
    @GetMapping("/findAll")
    public Result findAll(){
        List<User> userList = userService.findAll();
        return new Result(true,20000,"查询成功",userList);
    }

    @RoleAuth(value = "ROLE_ADMIN")
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user){
        try {
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,20000,"更新成功");
    }
}
